﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections.Concurrent;
using System.Threading;
using System.ServiceModel;
using CRM.Index.Box;
using CRM.Index.IProvider;
using CRM.Entity;
using CRM.Entity.Extra;
using CRM.Entity.Page;

namespace CRM.Index.Provider
{
   [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public partial class ProviderService : IProviderService
    {
       public static ReaderWriterLockSlim  userInfo_rws = new ReaderWriterLockSlim();
       
        #region 插入操作
        /// <summary>
        /// 插入操作
        /// </summary>
        /// <param name="UserInfo"></param>
        public bool UserInfo_Insert(UserInfo userInfo)
        {
          try
            {
                userInfo_rws.EnterWriteLock();
                
                DictionaryBox.userInfoDictionary.Add(userInfo.UserId, userInfo);

                DictionaryBox.userInfoUserIdSort.Add(userInfo.UserId);
                
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                
                return false;
            }
            finally
            {
                userInfo_rws.ExitWriteLock();
            }
        }
        #endregion

        #region 更新操作
        /// <summary>
        /// 更新操作
        /// </summary>
        /// <param name="UserInfo"></param>
        public bool UserInfo_Update(UserInfo userInfo)
        {
          try
            {
                userInfo_rws.EnterWriteLock();

                DictionaryBox.userInfoDictionary[userInfo.UserId] = userInfo;
                
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                
                return false;
            }
            finally
            {
                userInfo_rws.ExitWriteLock();
            }
        }
        #endregion

        #region 删除操作
        /// <summary>
        /// 删除操作
        /// </summary>
        /// <param name="UserInfo"></param>
        public bool UserInfo_Delete(UserInfo userInfo)
        {
          try
            {
                userInfo_rws.EnterWriteLock();

                DictionaryBox.userInfoDictionary.Remove(userInfo.UserId);

                DictionaryBox.userInfoUserIdSort.Remove(userInfo.UserId);
                
                return true;
            }  
            catch (Exception e)
            {
                Console.WriteLine(e.Message);

                return false;
            }
            finally
            {
                userInfo_rws.ExitWriteLock();
            }
        }
        #endregion
        
        #region 根据主键获取一条
        /// <summary>
        /// 根据主键获取一条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public UserInfo UserInfo_Single(Int64 id)
        {
            try
            {
                userInfo_rws.EnterReadLock();

                return DictionaryBox.userInfoDictionary[id];
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
            finally
            {
                userInfo_rws.ExitReadLock();
            }
        }


        public UserInfo UserInfo_SingleByName(string userName)
        {
            try
            {
                userInfo_rws.EnterReadLock();

                return DictionaryBox.userInfoDictionary.Values.FirstOrDefault(t => t.UserName == userName);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
            finally
            {
                userInfo_rws.ExitReadLock();
            }
        }
        #endregion
        
        #region 获取TopK条
        /// <summary>
        /// 获取TopK条
        /// </summary>
        /// <param name="top"></param>
        public UserInfoPage UserInfo_Top(int top)
        {
            try
            {
                 userInfo_rws.EnterReadLock();

                var  userInfoPage = new  UserInfoPage(){ ListEntity = new List<UserInfo>() };

                var skip = DictionaryBox.userInfoUserIdSort.Count - top < 0 ? 0 : DictionaryBox.userInfoUserIdSort.Count - top;

                var listID = DictionaryBox.userInfoUserIdSort.Skip(skip).Take(top).ToList();

                for (int i = listID.Count - 1; i >= 0; i--)
                {
                    userInfoPage.ListEntity.Add(DictionaryBox.userInfoDictionary[listID[i]]);
                }

                userInfoPage.RecordCount = DictionaryBox.userInfoUserIdSort.Count;

                return userInfoPage;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
            finally
            {
                 userInfo_rws.ExitReadLock();
            }
        }
        #endregion

        #region 分页获取条数
        /// <summary>
        /// 分页获取条数
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public UserInfoPage UserInfo_Page(PageInfo info)
        {
            try
            {
                userInfo_rws.EnterReadLock();

                var userInfoPage = new UserInfoPage() { ListEntity = new List<UserInfo>() };

                var skip = DictionaryBox.userInfoUserIdSort.Count - (info.PageIndex * info.PageSize);

                info.PageSize = skip < 0 ? skip + info.PageSize : info.PageSize;

                var listID = DictionaryBox.userInfoUserIdSort.Skip(skip).Take(info.PageSize).ToList();

                for (int i = listID.Count - 1; i >= 0; i--)
                {
                    userInfoPage.ListEntity.Add(DictionaryBox.userInfoDictionary[listID[i]]);
                }

                userInfoPage.RecordCount = DictionaryBox.userInfoUserIdSort.Count;

                return userInfoPage;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
            finally
            {
                userInfo_rws.ExitReadLock();
            }
        } 
        #endregion
        
        #region 分页获取条数
        /// <summary>
        /// 分页获取条数
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public UserInfoExtraPage UserInfoExtra_Page(PageInfo info)
        {
            try
            {
                userInfo_rws.EnterReadLock();

                var userInfoExtaPage = new UserInfoExtraPage() { ListEntity = new List<UserInfoExtra>() };

                var skip = DictionaryBox.userInfoUserIdSort.Count - (info.PageIndex * info.PageSize);

                info.PageSize = skip < 0 ? skip + info.PageSize : info.PageSize;

                var listID = DictionaryBox.userInfoUserIdSort.Skip(skip).Take(info.PageSize).ToList();

                for (int i = listID.Count - 1; i >= 0; i--)
                {
                    var single = DictionaryBox.userInfoDictionary[listID[i]];

                    var userInfoExtra = new UserInfoExtra()
                    {
                        UserId=single.UserId,
                        UserName=single.UserName,
                        Password=single.Password,
                        UserType=single.UserType,
                        CreateTime=single.CreateTime,
                        LastLoginTime=single.LastLoginTime,
                        Disable=single.Disable,
                        UserTypeSingle =DictionaryBox.userTypeDictionary.ContainsKey(single.UserType)?DictionaryBox.userTypeDictionary[single.UserType]:new UserType()
                    };

                    userInfoExtaPage.ListEntity.Add(userInfoExtra);
                }

                userInfoExtaPage.RecordCount = DictionaryBox.userInfoUserIdSort.Count;

                return userInfoExtaPage;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
            finally
            {
                userInfo_rws.ExitReadLock();
            }
        }
        #endregion
    }
}
